<?php

namespace App\Admin\Extensions\RentClothOrder;

use Encore\Admin\Admin;

class SuccessLine
{
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function script()
    {
        return <<<SCRIPT
        //隐藏的点击事件
        $(document).on('click', '.btn-success', function() {
            //获取的ID
            var data;
            var id = $(this).data('id');
            $.post("/admin/brands/get_brand",{_token:LA.token},function(data){
               swal({
                    title: '请选择店铺品牌',
                    input: 'select',
                    inputOptions: data,
                    inputPlaceholder: '请选择店铺品牌',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    cancelButtonText: '取消',
                    confirmButtonText: '确认'
                }).then((result) =>{
                    if(result.value){
                        $.ajax({
                            type: 'post',
                            url: '/admin/rentclothorderline/success',
                            data: {
                                id:id,
                                brand:result.value,
                                _token:LA.token,
                            },
                            success: function (data) {
                                $.pjax.reload('#pjax-container');
                                if (typeof data === 'object') {
                                    if (data.code == 1) {
                                        swal(data.msg, '', 'success');
                                    } else {
                                        swal(data.msg, '', 'error');
                                    }
                                }
                            }
                        });
                    }else{
                        Swal({
                            type: 'error',
                            title: '错误',
                            text: '请选择店铺品牌!',
                        })
                    }
                })
            });
        });




SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "<button type='button' class='btn btn-success' data-id='{$this->id}'>通过</button>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
